﻿#region copyright info
//------------------------------------------------------------------------------
// <copyright company="ChaosStudio">
//     Copyright (c) 2002-2010 巧思工作室.  All rights reserved.
//     Contact:		MSN:zhouyu@cszi.com , QQ:478779122
//		Link:				http://www.69sea.com
// </copyright>
//------------------------------------------------------------------------------
#endregion

using System.Web;

namespace CS.Utility.Http
{
    /// <summary>
    ///  Httt用户访问时的想关信息获取[访客信息]
    ///  无法继承
    /// </summary>
    /// 
    /// <description class = "CS.Utility.HttpUserUtility">
    ///   
    /// </description>
    /// 
    /// <history>
    ///   2010-2-2 13:11:40 , zhouyu ,  创建	     
    ///  </history>
    public sealed class Guest
    {

        #region Language 

        /// <summary>
        /// 当前语言，默认：en-us
        /// </summary>
        public static string Language
        {
            get
            {
                //默认语言是英文
                var current = HttpContext.Current;
                if (current == null || current.Request.UserLanguages.Length == 0) return "en-us";
                return current.Request.UserLanguages[0];
            }
        }

        #endregion

        #region IpAddress

        /// <summary>
        /// 获取用户IP
        /// </summary>
        /// <returns></returns>
        public static string IpAddress()
        {
            bool tmp;
            return IpAddress(out tmp);
        }
        /// <summary>
        /// 获取IP地址(IP地址仅供参考，不可做为判断的依据)
        /// <para>针对公司启用反向代理后的写法</para>
        /// </summary>
        /// <param name="isProxy">是否通过代理访问</param>
        /// <returns>IP地址</returns>
        public static string IpAddress(out bool isProxy)
        {
            var current = HttpContext.Current;
            const string noIp = "Unknow";
            if(current ==null)
            {
                isProxy = false;
                return noIp;
            }
            //Note:HTTP_X_FORWARDED_FOR(如果是代理这个是代理的IP)是可由客户端修改IP地址的
            var forIp = current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];  //本机真实IP,1层代理IP,2层代理IP,.....
            isProxy = current.Request.ServerVariables["HTTP_VIA"] != null ? true : false;
            if(!string.IsNullOrEmpty(forIp))
            {
                if (!isProxy)
                    isProxy = forIp.IndexOf(',') > -1 ? true : false;

                if(forIp.IndexOf('.') > 0)
                    return forIp;
        
            }
            var lastIp = current.Request.UserHostAddress ?? noIp;
            return lastIp;
                    
            #region 以前老的版本
            //const string noIp = "0.0.0.0";
            //var current = HttpContext.Current;
            //if (current == null)
            //{
            //    isProxy = false;
            //    return noIp;
            //}
            ////Note:HTTP_X_FORWARDED_FOR(如果是代理这个是代理的IP)是可由客户端修改IP地址的
            //var ip = current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? noIp;
            //isProxy = ip.IndexOf(',') > -1 ? true : false;
            //if (isProxy || current.Request.ServerVariables["HTTP_VIA"] != null)
            //{
            //    return ip;
            //}
            //var lastIp = current.Request.UserHostAddress ?? noIp;
            ////var lastIp = current.Request.ServerVariables["REMOTE_ADDR"] ?? noIp;     //REMOTE_ADDR 最后一个代理的IP(有代理的情况下)
            //return lastIp;
            #endregion
        }

        #endregion

        #region MacAddress

        ///// <summary>
        ///// 获得Mac地址
        ///// <para>太慢了，并且必须要安NetBios协议</para>
        ///// </summary>
        ///// <returns></returns>
        //public static string MacAddress()
        //{
        //    var ip = IpAddress();
        //    var mac = "";
        //    var p = new System.Diagnostics.Process
        //                {
        //                    StartInfo =
        //                        {
        //                            FileName = "nbtstat",
        //                            Arguments = "-a " + ip,
        //                            UseShellExecute = false,
        //                            CreateNoWindow = true,
        //                            RedirectStandardOutput = true
        //                        }
        //                };
        //    p.Start();
        //    var output = p.StandardOutput.ReadToEnd();
        //    var len = output.IndexOf("MAC Address = ");
        //    if (len > 0)
        //    {
        //        mac = output.Substring(len + 14, 17);
        //    }
        //    p.WaitForExit();
        //    return mac;
        //}

        #endregion

        #region UrlReferrer

        /// <summary>
        /// 获取上一页地址
        /// </summary>
        /// <returns></returns>
        public static string UrlReferrer()
        {
            var current = HttpContext.Current;
            return (current == null || current.Request.UrlReferrer ==null) ? string.Empty : current.Request.UrlReferrer.ToString();
        }

        #endregion

       


    }
}